<?php
//check1
namespace Home\Controller;
use Think\Controller;
use Think\Model;
use Home\Model\User;

class SregisterController extends Controller {
    public function index(){
    	if(session('?user')){
    		$this->assign('user',I('session.user'));
    		redirect(U('Home/logout'));
    	}
    	else
    		$this->display();
    }
    
    public function register(){
    	if(IS_POST){
	    	$user = D('Userstudent');
	    	$data['name'] = I('post.username');
	    	$data['password'] = I('post.password');
	    	$data['psw2'] = I('post.password2');
	    	$data['email'] = I('post.email');
	    	$check['email']=I('post.email');
	    	$data['university'] = I('post.university');
	    	if ($data1=$user->create($data)){
	    		$registered=$user->where($check)->select();
	    		if($registered){
	    			$error='此邮箱已经注册，请直接登录！';
	    			$this->assign('message','此邮箱已经注册，请直接登录！');
	    			$this->assign('pagename','登录页面');
	    			$this->assign('data',$error);
	    			$this->error('请直接登录！', '../Login/index',3);	
	    		}
	    		else{
		    		if($user->add($data1)){
		    			session('user', $data['email']);
		    			$test="
		    			<html>
		    			<head>
		    			<title>Welcome to Brilliance Finance</title>
		    			</head>
		    			<body>
		    			<p>Hi,</p>
		    			<p></p>
		    			<p>Thank you for registering with Brilliance Finance and welcome to our elite community.</p>
		    			<p>If you have any questions regarding your study loan application, please feel free to contact
		    			us as info@brilliancefinance.com, or call us at 86-010-56422795. In addition, you could check out
		    			our Q&A session at www.brilliancefinance.com/Questions</p>
		    			
		    			<p>We wish you the greatest success in your study and your future career!</p>
		    			<p></p>
		    			<p>Best Regards,</p>
		    			<p>The Brilliance Finance Team</p>
		    			
		    			</body>
		    			</html>";
		    			SendMail($data['email'],'Welcome to Brilliance Finance',$test);		
		    			redirect(U('Abasics/index'));
		    		}
		    		else{
		    			$error=$user->getError();
		    			$this->assign('message','注册失败');
		    			$this->assign('pagename','注册页面');
		    			$this->assign('data',$error);
		    			$this->error('注册失败！','index',3);		
		    		}
		    	}
	    	}
		    else{
		   		$error=$user->getError(); 
		   		$this->assign('message','注册失败');
	    		$this->assign('pagename','注册页面');
		   		$this->assign('data',$error);
	    		$this->error('注册失败！', 'index',3);		
	    	} 
    	}
    	else{
    		$this->display('index');  		
    	}	    	
    } 
}